
global datapath "~/DataFolder"
global Analyses "~/Results"

********************************************** Table 9: % Conventional Mortgages 
use "$datapath/RegData.dta", clear
keep rssdhcr fips year raw_num raw_volume total_applications spec_above5pctemp riskiness male avg_minority branch distance numsbl lretain size loan deposit p_ci p_re roa liquid  county_year bank_state

rename raw_num raw_num_con
rename raw_volume raw_volume_con
rename total_applications total_applications_con
rename riskiness riskiness_con
rename male male_con
rename avg_minority avg_minority_con

merge 1:1 rssdhcr year fips using "$datapath/RegData_gov.dta", keepusing(raw_num raw_volume total_applications spec_above5pctemp riskiness male avg_minority branch distance numsbl lretain size loan deposit p_ci p_re roa liquid county_year bank_state)

keep if year>=1999 & year<=2017

rename raw_num raw_num_gov
rename raw_volume raw_volume_gov
rename total_applications total_applications_gov
rename riskiness riskiness_gov
rename male male_gov
rename avg_minority avg_minority_gov

replace raw_num_gov = 0 if missing(raw_num_gov)
replace raw_volume_gov = 0 if missing(raw_volume_gov)
replace total_applications_gov = 0 if missing(total_applications_gov)
replace raw_num_con = 0 if missing(raw_num_con)
replace raw_volume_con = 0 if missing(raw_volume_con)
replace total_applications_con = 0 if missing(total_applications_con)

gen p_num_gov = raw_num_gov / (raw_num_gov+raw_num_con)
gen p_vol_gov = raw_volume_gov / (raw_volume_gov+raw_volume_con )
gen p_num_con = raw_num_con / (raw_num_gov+raw_num_con)
gen p_vol_con = raw_volume_con / (raw_volume_gov+raw_volume_con )

gen weight_gov = total_applications_gov / (total_applications_gov+total_applications_con)
gen weight_con = total_applications_con / (total_applications_gov+total_applications_con)

gen riskiness = .
gen male = .
gen avg_minority = .
foreach var of varlist riskiness male avg_minority{
	replace `var' = `var'_gov if missing(`var'_con) & !missing(`var'_gov)
	replace `var' = `var'_con if !missing(`var'_con) & missing(`var'_gov)
    replace `var' = `var'_gov*weight_gov + `var'_con*weight_con	if !missing(`var'_gov) & !missing(`var'_con)	
	
	gen `var'_num = `var'_gov if missing(`var'_con) & !missing(`var'_gov)
	replace `var'_num = `var'_con if !missing(`var'_con) & missing(`var'_gov)
    replace `var'_num = `var'_gov*p_num_gov + `var'_con*p_num_con if !missing(`var'_gov) & !missing(`var'_con)
	
	gen `var'_vol = `var'_gov if missing(`var'_con) & !missing(`var'_gov)
	replace `var'_vol = `var'_con if !missing(`var'_con) & missing(`var'_gov)
    replace `var'_vol = `var'_gov*p_vol_gov + `var'_con*p_vol_con if !missing(`var'_gov) & !missing(`var'_con)
}

global controls riskiness male avg_minority branch distance numsbl lretain size loan deposit p_ci p_re roa liquid


reghdfe p_num_con spec_above5pctemp , absorb(rssdhcr year fips) cluster(fips)
outreg2 using Table9.xls, replace label dec(3) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(Year FE, Yes, Bank FE, Yes, County FE, Yes, County*Year FE, No, Bank*State FE, No) 

reghdfe p_num_con spec_above5pctemp $controls, absorb(rssdhcr year fips) cluster(fips)
outreg2 using Table9.xls, append label dec(3) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(Year FE, Yes, Bank FE, Yes, County FE, Yes, County*Year FE, No, Bank*State FE, No) 

reghdfe p_num_con spec_above5pctemp $controls, absorb(county_year bank_state) cluster(fips)
outreg2 using Table9.xls, append label dec(3) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(Year FE, No, Bank FE, No, County FE, No, County*Year FE, Yes, Bank*State FE, Yes) 

reghdfe p_vol_con spec_above5pctemp , absorb(rssdhcr year fips) cluster(fips)
outreg2 using Table9.xls, append  label dec(3) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(Year FE, Yes, Bank FE, Yes, County FE, Yes, County*Year FE, No, Bank*State FE, No) 

reghdfe p_vol_con spec_above5pctemp $controls, absorb(rssdhcr year fips) cluster(fips)
outreg2 using Table9.xls, append label dec(3) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(Year FE, Yes, Bank FE, Yes, County FE, Yes, County*Year FE, No, Bank*State FE, No) 

reghdfe p_vol_con spec_above5pctemp $controls, absorb(county_year bank_state) cluster(fips)
outreg2 using Table9.xls, append label dec(3) nor2 stats(coef se) addstat(Adjusted R-squared,e(r2_a)) addtext(Year FE, No, Bank FE, No, County FE, No, County*Year FE, Yes, Bank*State FE, Yes) 





